﻿
using DataWorks_Sqlite.MappingClass;
using DataWorks_Tools.BasicMethods;
using DataWorks_Tools.MappModals.CalInputModals;
using DataWorks_Tools.MappModals.CalSaveModals;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static DataWorks_Tools.BasicMethods.ReadfromCSV;

namespace DataWorks_Tools.HalfHourCalSave.Common.Basic
{
    /// <summary>
    /// 通用方法类
    /// </summary>
    public static class CommonMehtods
    {
        /// <summary>
        /// 通用清零方法（带委托）
        /// </summary>
        /// <param name="list">需要清零的数据</param>
        /// <param name="func1">清零的标准委托方法</param>
        ///  /// <param name="func2">清零的标准判断委托方法</param>
        public static async Task DoZeroDelAsync(this List<double> list,  Func<byte> func1, Func<double,byte,bool> func2)
        {
           await Task.Run(() =>
            {
                byte zero = func1();
                for (int i = 0; i < list.Count; i++)
                {
                    if (func2(list[i],zero))
                    {
                        list[i] = 0;
                    }
                }
            });
        }

        /// <summary>
        /// 通用识别刹车或油门的方法（带委托）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="csvimport">csv数据所有列</param>
        /// <param name="dia_Props_Admin">公共参数</param>
        /// <param name="dia_Props_User">用户参数</param>
        /// <param name="func1">特定列名称委托</param>
        /// <param name="func2">清零参数委托</param>
        /// <param name="func3">清零判断委托</param>
        /// <param name="func4">添加工况类实例委托</param>
        /// <returns> List<T></returns>
        public static async Task< List<T>> GetConditonDelAsync<T>(CSVImport csvimport,dw_props_admin dia_Props_Admin, HalfHourCalInputModel dia_Props_User, Func<string> listfunc, Func<string> accfunc, Func<byte> zerofunc, Func<double, byte, bool> zeroiffunc, Func<List<double>, List<double>, List<double>, int, int, T> addfunc) where T : calsave_base_statistic, new()
        {
            //筛选需要的通道
            var conditionlist = await GetSpecifiedColumnDataDelAsync(csvimport, listfunc);
            var speedlist = await GetSpecifiedColumnDataDelAsync(csvimport, () => { return dia_Props_User.chantitle_spd.ReName();});
            var accbodylist = await GetSpecifiedColumnDataDelAsync(csvimport, accfunc);
            await conditionlist.DoZeroDelAsync(zerofunc, zeroiffunc);
            List<T> returnlist = new List<T>();
            int start = 0, end;//识别刹车开始点数和结束点数
            for (int i = 0; i < conditionlist.Count - 1; i++)
            {
                if (conditionlist[i] == 0 && conditionlist[i + 1] != 0)//这里list数量大于speedlistcount，所以会溢出
                {
                    start = i + 1;
                }
                if (conditionlist[i] != 0 && conditionlist[i + 1] == 0 && start != 0)//这里一定要先找到start才开始找end，否则一开始可能先找到end就不对了。这里的Start不等于0是要保证上面的if要能执行，也就是上面先要找到start才执行下面的语句
                {
                    end = i;
                    List<double> acctemp = new List<double>();
                    List<double> postemp = new List<double>();
                    List<double> spdtemp = new List<double>();
                    for (int j = start; j < end; j++)
                    {
                        acctemp.Add(accbodylist[j]);
                        postemp.Add(conditionlist[j]);
                        spdtemp.Add(speedlist[j]);
                    }
                    if (acctemp.Count > dia_Props_Admin.lastingpoints)
                    {
                        //加速度，特定通道数据，速度数据，起始idx，终止idx
                        var result = addfunc(acctemp, postemp, spdtemp, start, end);
                        returnlist.Add(result);
                    }
                    start = 0;
                }
            }
            return returnlist;
        }

     
    }
}
